package com.hnqcgczy.supplier.dao.impl;

import com.hnqcgczy.supplier.dao.AdminDao;
import com.hnqcgczy.supplier.db.MyTransaction;
import com.hnqcgczy.supplier.utils.BaseDao;
import com.hnqcgczy.supplier.vo.Admin;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AdminDaoImpl extends BaseDao implements AdminDao {
    //通过构造方法把事物传进来
    public AdminDaoImpl(MyTransaction myTransaction) {
        super(myTransaction);
    }

    @Override
    public boolean getUserByName(String userName) throws SQLException {
        //1.写sql语句
        String sql = "select admin_id,admin_name,admin_password,admin_role,phone,email from t_admin where admin_name = ?";
        //2.调用查询的公共方法
        ResultSet rs = super.query(sql, userName);
        //3.返回结果,boolean为假的时候代表已经被注册，true代表没有被注册
        boolean flag = true;
        while (rs.next()) {
            flag = false;
        }
        super.closeResource();
        //4.关闭资源
        return flag;
    }

    @Override
    public boolean reg(Admin admin) throws SQLException {
        //1.准备sql语句
        String sql = "INSERT INTO `t_admin`(`admin_id`, `admin_name`, `admin_password`, `admin_role`, `phone`, `email`) VALUES (null, ?, ?, ?, ?, ?)";
        //2.调用公共方法update
        int i = super.update(sql, admin.getAdminName(), admin.getAdminPassword(), admin.getAdminRole(), admin.getPhone(), admin.getEmail());
        //3.关闭资源
        super.closeResource();
        return i > 0;
    }

    @Override
    public Admin login(String username, String password) throws SQLException {
        //1.准备sql
        String sql = "select admin_id,admin_name,admin_password,admin_role,phone,email from t_admin where admin_name = ? and admin_password = ? ";
        //2.调用公共方法query
        ResultSet rs = super.query(sql, username, password);
        //3.返回处理
        if (rs != null) {
            while (rs.next()) {
                Admin temp = new Admin();
                //将数据库中查出来的内容封装到实体对象中
                temp.setAdminId(rs.getInt("admin_id"));
                temp.setAdminName(rs.getString("admin_name"));
                temp.setAdminPassword(rs.getString("admin_password"));
                temp.setAdminRole(rs.getInt("admin_role"));
                temp.setPhone(rs.getString("phone"));
                temp.setEmail(rs.getString("email"));
                return temp;
            }
        }
        //4.关闭资源
        super.closeResource();
        return null;
    }

    @Override
    public boolean updateById(Admin admin) throws SQLException {
        //1.编写sql
        String sql = "UPDATE `t_admin` SET `admin_name` =?, `admin_password` = ?, `admin_role` = ?, `phone` = ?, `email` = ? WHERE `admin_id` = ?";
        //2.调用公共方法update
        int i = super.update(sql, admin.getAdminName(), admin.getAdminPassword(), admin.getAdminRole(), admin.getPhone(), admin.getEmail(), admin.getAdminId());
        //3.关闭资源
        super.closeResource();
        return i > 0;
    }

    @Override
    public boolean deleteById(Integer id) throws SQLException {
        //1.准备sql
        String sql = "delete from t_admin where admin_id = ?";
        //2.调用公共方法update
        int i = super.update(sql, id);
        //3.关闭资源
        super.closeResource();
        return i > 0;
    }

    @Override
    public List<Admin> queryAdmin(Admin admin) throws SQLException {
        //第一：拼sql
        StringBuffer sb = new StringBuffer("select admin_id,admin_name,admin_password,admin_role,phone,email from t_admin where 1=1 ");
        if (admin != null) {
            if (admin.getAdminId() != null && !"".equals(admin.getAdminId()) && admin.getAdminId() != 0) {
                sb.append(" and admin_id = " + admin.getAdminId());
            }
            if (admin.getAdminName() != null && !"".equals(admin.getAdminName())) {
                //select * from t_admin where admin_name like '%u%'
                sb.append(" and admin_name like '%" + admin.getAdminName() + "%'");
            }
            //如果还有要查询的条件可以继续写
        }
        //第二：调查询的公共方法
        List<Admin> lists = new ArrayList<Admin>();
        String sql = sb.toString();
        ResultSet rs = super.query(sql);
        if (rs != null) {
            while (rs.next()) {
                Admin temp = new Admin();
                //将数据库中查出来的内容封装到实体对象中
                temp.setAdminId(rs.getInt("admin_id"));
                temp.setAdminName(rs.getString("admin_name"));
                temp.setAdminPassword(rs.getString("admin_password"));
                temp.setAdminRole(rs.getInt("admin_role"));
                temp.setPhone(rs.getString("phone"));
                temp.setEmail(rs.getString("email"));
                lists.add(temp);
                temp = null;
            }
        }
        return lists;
    }

    @Override
    public Admin getAdminById(Integer id) throws SQLException {
        //1.准备sql
        String sql = "select admin_id,admin_name,admin_password,admin_role,phone,email from t_admin where admin_id = ? ";
        //2.调用公共方法query
        ResultSet rs = super.query(sql, id);
        //3.返回处理
        if (rs != null) {
            while (rs.next()) {
                Admin temp = new Admin();
                //将数据库中查出来的内容封装到实体对象中
                temp.setAdminId(rs.getInt("admin_id"));
                temp.setAdminName(rs.getString("admin_name"));
                temp.setAdminPassword(rs.getString("admin_password"));
                temp.setAdminRole(rs.getInt("admin_role"));
                temp.setPhone(rs.getString("phone"));
                temp.setEmail(rs.getString("email"));
                return temp;
            }
        }
        //4.关闭资源
        super.closeResource();
        return null;
    }
}
